﻿namespace SubhadraSolutions.Sharp.Utils.Threading
{
    /// <summary>
    /// The DispatcherDependentBasedDispatcherProvider class.
    /// </summary>
    public sealed class DispatcherDependentBasedDispatcherProvider : IDispatcherProvider
    {
        /// <summary>
        /// The instance object
        /// </summary>
        private static readonly DispatcherDependentBasedDispatcherProvider InstanceObject = new DispatcherDependentBasedDispatcherProvider();

        /// <summary>
        /// Prevents a default instance of the <see cref="DispatcherDependentBasedDispatcherProvider"/> class from being created.
        /// </summary>
        private DispatcherDependentBasedDispatcherProvider()
        {
        }

        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <value>
        /// The instance.
        /// </value>
        public static DispatcherDependentBasedDispatcherProvider Instance
        {
            get
            {
                return InstanceObject;
            }
        }

        /// <summary>
        /// Gets the dispatcher.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <returns></returns>
        public IDispatcher GetDispatcher(object target)
        {
            var dispatcherDependent = target as IDispatcherDependent;
            if (dispatcherDependent != null)
            {
                return dispatcherDependent.GetDispatcher();
            }

            return null;
        }
    }
}
